package cn.zbh.basic.controller;

import cn.zbh.basic.util.AjaxResult;
import cn.zbh.basic.util.FastDfsUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
@RequestMapping("/fastdfs")
@RestController
public class FastDfsController {

    /**
     *
     * RequestPart:接收文件
     *
     * ,required = true：必须上传图片
     * @param file
     * @return
     */
    @PostMapping
    public AjaxResult upload(@RequestPart(value = "file",required = true)MultipartFile file){
        try {
            //获取原始文件名称
            String filename = file.getOriginalFilename();
            //获取后缀不要.
            String substring = filename.substring(filename.lastIndexOf("/") + 1);
            //路劲
            String path = FastDfsUtils.upload(file.getBytes(), substring);
            System.out.println(path);
            return AjaxResult.me().setResultObj(path);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  AjaxResult.me().setMsg("上传失败");
    }

    @DeleteMapping
    //       "/common/fastDfs/?path="+filePath
    public AjaxResult delete(String path){
        try {
            String st = path.substring(1);
            String s = st.substring(0, st.indexOf("/"));
            String st2 = st.substring(st.lastIndexOf("/") + 1);
            FastDfsUtils.delete(s,st2);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("删除失败");
        }
    }
}
